package org.hikari.qqbot.xiuxian.util;

import java.util.Date;

/**
 * 过期缓存
 * created at 2025-01-01 15:07:59
 *
 * @author yinchao
 * @version 1.0
 **/
public class TimedCache<V> {

    public static final TimedCache<Void> EMPTY = new TimedCache<>(null, Long.MIN_VALUE);

    @SuppressWarnings("unchecked")
    public static <T> TimedCache<T> empty() {
        return (TimedCache<T>) EMPTY;
    }

    private final V value;
    private final long expireTime;

    public TimedCache(V value, long duration) {
        this.value = value;
        this.expireTime = System.currentTimeMillis() + duration * 1000L;
    }

    public TimedCache(V value, Date expireTime) {
        this.value = value;
        this.expireTime = expireTime.getTime();
    }

    public V getValue() {
        if (expireTime <= 0) {
            return null;
        }
        if (System.currentTimeMillis() >= expireTime) {
            return null;
        }
        return this.value;
    }

}
